package Concurrent;

import java.util.concurrent.atomic.AtomicInteger;

public class Automic {
    public static void main(String[] args) throws InterruptedException {
        //这些原子类 , 就是基于CAS 实现了 自增 自减的操作(这些操作都是原子的),此时进行这一类操作,不会出现线程安全问题
        AtomicInteger count = new AtomicInteger(0);
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count.getAndIncrement();   //自增
                //count.decrementAndGet();  //自减
            }
        } );
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                count.getAndIncrement();   //自增
                //count.decrementAndGet();  //自减
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        //通过get()获取到内部的数字
        System.out.println("结果为 : " + count.get());
    }
}
